<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){

        $this->display();
    }
    // 用户列表页
    public function user_list(){
    	$User = M('user');
        $count = $User->count();
        $Page = new \Think\Page($count,2);
        $show = $Page->show();
        $user_list = $User->order('user_id')->limit($Page->firstRow.','.$Page->listRows)->select();
    	// print_r($show);die;

    	$data = array(
            'show' => $show,
    		'user_list' => $user_list
    		);
    	$this->assign($data);
        $this->display();
    }
    // 用户添加
    public function user_add(){
    	if (IS_POST) {
    		$data['user_account'] = I('post.account');
    		$data['user_name'] = I('post.name');
    		$data['user_age'] = I('post.age');
    		$data['user_sex'] = I('post.sex');
    		$data['user_tle'] = I('post.tle');
    		$data['user_email'] = I('post.email');
    		$data['user_realname'] = I('post.realname');
    		$password = password(I('post.password'));
    		$data['user_password'] = $password['password'];
    		$data['user_sait'] = $password['salt'];
    		// print_R($password);die;
    		$upload = new \Think\Upload();
    		$upload->maxSize   =     3145728 ; 
    		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg'); 
    		$upload->rootPath  =    THINK_PATH;
    		$upload->savePath  =      '../Public/Home/uploads/';
    		$upload->subName   =    array('date','Ymd');
    		$info   =   $upload->uploadOne($_FILES['photo']);
    		if (!$info) {
    			$this->error($upload->getError());exit;
    		}
    		$data['user_img'] = date('Ymd').'/'.$info['savename'];
    		// print_r($data);die;
    		$User = M('user');
    		$user_add = $User->data($data)->add();
    		    if(!$user_add) {  
    		    $this->error('添加失败');exit;
    		    }else{     
    		    $this->success('添加成功','/Admin/Index/user_list');exit;
    		  }
    	}

        $this->display();
    }
    // 用户编辑
    public function user_edit(){
        $id = I('get.id');
        $User = M('user');
        $user = $User->where(array('user_id'=>$id))->find();
        // print_r($user);die;
        if (IS_POST) {

        }
        $this->display();
    }
    // 删除用户
    public function user_del(){
        $User = M('user');
        $conf = C(TMPL_PARSE_STRING);
        if (IS_GET) {
            $id = I('get.id');
            $user_del = $User->where(array('user_id'=>$id))->find();
            $res = $User->where(array('user_id'=>$id))->delete(); 
            if (!$res) {
                $this->error('删除失败');exit;
            }else{   
                unlink($conf['__UPLOADS__'].$user_del['user_img']);    
                $this->success('删除成功','/Admin/Index/user_list');exit;
            }
        }
        if (IS_POST) {
            $idarr = I('post.idarr');
            $i = 0;
            foreach ($idarr as $key => $value) {
                $user_del = $User->where(array('user_id'=>$value))->find();
                $res = $User->where(array('user_id'=>$value))->delete();
                unlink($conf['__UPLOADS__'].$user_del['user_img']); 
                $i++;
            }
            if (!$res) {
                $this->error('删除失败');exit;
            }else{   
                $this->success('成功删除'.$i.'条数据','/Admin/Index/user_list');exit;
            }
        }
    }
}